﻿using System;
using System.IO;
using System.Web;
using XWG.JWBST.ProcessInterfaces;
using Newtonsoft.Json.Linq;
using XWG.JWBST.JWBSTEntity;
using Newtonsoft.Json;

//接口废弃  2013年7月17日 14:47:09
namespace XWG.JWBST.OnlineAlarmProcess
{
    /// <summary>
    /// 在线报警
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 在线报警
        /// </summary>
        /// <param name="reader">接受的数据</param>
        /// <param name="context">请求信息</param>
        /// <returns>操作结果</returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            // 读取提交数据
            string receivemes = reader.ReadToEnd();
            bool issuccess = false;
            // UrlDecode解码
            receivemes = HttpUtility.UrlDecode(receivemes);

            // 反序列化json对象
            JObject oka = (JObject)JsonConvert.DeserializeObject(receivemes);
            TbBstOnlineAlarm onLine = new TbBstOnlineAlarm();

            onLine.OnlineAlarmPkid = Guid.NewGuid().ToString();
            onLine.UserId = oka["userID"].ToString().Replace("\"", string.Empty);
            onLine.PhoneNo = oka["phone"].ToString().Replace("\"", string.Empty);
            onLine.GpsX = oka["GPS_JD"].ToString().Replace("\"", string.Empty);
            onLine.GpsY = oka["GPS_WD"].ToString().Replace("\"", string.Empty);
            onLine.AlarmType = oka["type"].ToString().Replace("\"", string.Empty);
            onLine.AlarmMessage = oka["content"].ToString().Replace("\"", string.Empty);
            onLine.AlarmDate = DateTime.Now;
            onLine.ProcessResult = "未处理";

            //当为在线报警时，需填写手机号
            if (onLine.AlarmType == "01")
            {
                if (onLine.PhoneNo != string.Empty)
                {
                    issuccess = XWG.JWBST.JWBSTBLL.TbBstOnlineAlarmBLL.InsertGetID(onLine);
                    //执行操作 并返回结果
                    if (issuccess)
                    {
                        return onLine.OnlineAlarmPkid;
                    }
                    else
                    {
                        return "Error";
                    }
                }
                return "Error:请填写手机号";
            }

            //线索举报
            else
            {
                //执行操作 并返回结果
                if (issuccess)
                {
                    return onLine.OnlineAlarmPkid;
                }
                else
                {
                    return "Error";
                }
            }





            ////通过事物进行数据插入
            //List<object> items = new List<object>();
            //List<int> op = new List<int>();
            //try
            //{
            //    bool IsSucceed = false;
            //    items.Add(onLine);
            //    op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
            //    items.Add(onLineFlie);
            //    op.Add((int)XWG.Framework.AuthSPConst.OP.Create);
            //    //XWG.Framework.AuthBLL.Tran.TransOP(items, op);
            //    XWG.JWBST.JWBSTBLL.Tran.TransOP(ref IsSucceed,items, op);
            //    if (IsSucceed)
            //    {
            //        return "在线报警成功！";
            //    }
            //    else
            //    {
            //        return "在线报警失败";
            //    }
            //}
            //catch (Exception ex)
            //{
            //    throw;
            //}
        }
    }
}
